
function Model() {
    var M = {}
    M.materialData = {}
    M.configData = {}
    M.pic = []
    M.stateData = {
        picIndex: 0,
        scaleFactor: 1,
        containerSize: {
            x: 0,
            y: 0
        },
        initRectangle: {
            width: 0,
            height: 0
        },
        mouseEndPosition:{
            x:0,
            y:0
        },
        currentRectangle:undefined,
        mouseDownPoint:{
            x:undefined,
            y:undefined
        },
        mouseDownState:false,
        moveVector:{
            x:0,
            y:0
        }
    }
    return {
        
        nextPic() {
            if (M.stateData.picIndex < M.picData.length) {
                M.stateData.picIndex += 1
            } else {
                M.stateData.picIndex = 0
            }
        },
        lastPic() {
            if (M.stateData.picIndex > 0) {
                M.stateData.picIndex -= 1
            } else {
                M.stateData.picIndex = M.picData.length
            }
        },
        getStateData() {
            return M.stateData
        },
        getPicArr() {
            return M.pic
        },
        setMaterialData(params) {
            M.materialData = params
        },
        setScaleFactor(deltaY) {
            var factor = M.stateData.scaleFactor
            var currentFactor = deltaY * 0.001
            factor += currentFactor
            factor = Math.max(0.3,factor)
            factor = Math.min(1,factor)
            M.stateData.scaleFactor = factor
        },
        setCurrentRectangle(x,y,width,height,scaleFactor){
            M.stateData.currentRectangle = {
                x,y,width,height,scaleFactor
            }
        },
        setInitRectangle() {
            var picWidth,picHeight
            var img = M.pic[M.stateData.picIndex]
            var hx = window.canvasNode.width * img.height / img.width
            var h1 = window.canvasNode.height
            var w1 = window.canvasNode.width
            var h2 = img.height
            var w2 = img.width
            // 图片上图取图框尺寸与canvas尺寸的比值
            var k0
            if (hx > h1) {
                // 裁上下
                var hn = w2 * h1 / w1
                // picTop = (h2 - hn) / 2
                // picLeft = 0
                k0 = w2/w1
                picWidth = img.width
                picHeight = hn
            } else {
                // 裁左右
                // picTop = 0
                var wn = h2 * w1 / h1
                // picLeft = (w2 - wn) / 2
                k0 = h2/h1
                picWidth = wn
                picHeight = img.height
            }
            M.stateData.initRectangle = {
                width: picWidth ,
                height: picHeight
            }
            
            M.stateData.k0 = k0
        },
        setMouseEndPosition(width,height){
            M.stateData.mouseEndPosition = {
                x:width,
                y:height
            }
        },
        setPointDownPosition(x,y){
            M.stateData.mouseDownPoint = {
                x,y
            }
        },
        setMouseDownState(state){
            M.stateData.mouseDownState = state
        },
        setMoveVector(x,y){
            var x1 = M.stateData.mouseDownPoint.x
            var y1 = M.stateData.mouseDownPoint.y
            M.stateData.moveVector = {
                x:x-x1,
                y:y-y1
            }
        },
        setMoveVectorZero(){
            M.stateData.moveVector = {
                x:0,
                y:0
            }
        }
    }
}

var M = Model()

export default M